﻿using EcoSystem.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace EcoSystem
{
    public class AttachmentConfigAppService : CrudAppService<
        AttachmentConfig,
        AttachmentConfigDto,
        Guid,
        GetAttachmentConfigInput,
        CreateOrUpdateAttachmentConfigDto,
        CreateOrUpdateAttachmentConfigDto>, IAttachmentConfigAppService
    {
        public AttachmentConfigAppService(
            IRepository<AttachmentConfig, Guid> repository
            ) : base(repository)
        {
        }


        public override async Task<PagedResultDto<AttachmentConfigDto>> GetListAsync(GetAttachmentConfigInput input)
        {
            var query = (await Repository.GetQueryableAsync()).AsNoTracking()
                   .WhereIf(!input.BussinessType.IsNullOrWhiteSpace(), x => x.BussinessType.Equals(input.BussinessType))
                   .WhereIf(!input.GroupName.IsNullOrWhiteSpace(), x => x.GroupName.Equals(input.GroupName));
            var total = await query.CountAsync();
            var items = await query.OrderBy(x => x.Id).PageBy(input).ToListAsync();
            return new PagedResultDto<AttachmentConfigDto>()
            {
                Items = await MapToGetListOutputDtosAsync(items),
                TotalCount = total
            };
        }

        public async Task<List<AttachmentConfigDto>> GetAllListAsync(GetAttachmentConfigInput input)
        {
            var query = (await Repository.GetQueryableAsync()).AsNoTracking()
                   .WhereIf(!input.BussinessType.IsNullOrWhiteSpace(), x => x.BussinessType.Equals(input.BussinessType))
                   .WhereIf(!input.GroupName.IsNullOrWhiteSpace(), x => x.GroupName.Equals(input.GroupName));
            var items = await query.OrderBy(x => x.Id).ToListAsync();
            return await MapToGetListOutputDtosAsync(items);
        }
    }
}
